package demo0;

import java.util.Objects;

public class Test0 {
    public static void main1(String[] args)  throws CloneNotSupportedException{
        Student student1 = new Student(10);
        Student student2 = (Student) student1.clone();//  Student  object向下转型
        //protected native Object clone() throws CloneNotSupportedException;
        //不同包中的子类                             //处理异常
        //一般在子类中使用super
        System.out.println(student1.age);
        System.out.println(student2.age);
    }

    public static void main2(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student(10);
        student1.m.money = 19.9;
        Student student2 = (Student) student1.clone();

        System.out.println(student1.m.money);
        System.out.println(student2.m.money);

        student1.m.money = 29.9;
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);

    }

    public static void main3(String[] args) {
        Object obj1 = new Student(1);
        Object obj2 = new Money();
    }

    public static void main4(String[] args) {
        Student student1 = new Student(10);
        Student student2 = new Student(10);
        //System.out.println(student1==student2);
        System.out.println(student1.equals(student2));
    }

    public static void main(String[] args) {
        Student student1 = new Student(10);
        Student student2 = new Student(10);
        System.out.println(student1.hashCode());
        System.out.println(student2.hashCode());//每个类都需要重写hashCode,equals
    }
}


class Money implements Cloneable{
    public double money;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Money money1 = (Money) o;
        return Double.compare(money1.money, money) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(money);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

//对学生进行克隆
class Student implements Cloneable{//标记接口，证明当前的类可以被克隆
    public int age;
    public Money m = new Money();

    public Student(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(m, student.m);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, m);
    }

    //重写克隆方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student tmp = (Student) super.clone();//子类重写的object只是调用了父类的clone
        tmp.m = (Money) this.m.clone();
        return tmp;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                '}';
    }


}